/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.utfpr.view;

import br.edu.utfpr.dao.DaoCliente;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Exceptions;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import pojo.Cliente;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//br.edu.utfpr.view//TelaCliente//EN",
autostore = false)
@TopComponent.Description(preferredID = "TelaClienteTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "br.edu.utfpr.view.TelaClienteTopComponent")
@ActionReference(path = "Menu/Window" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_TelaClienteAction",
preferredID = "TelaClienteTopComponent")
@Messages({
    "CTL_TelaClienteAction=TelaCliente",
    "CTL_TelaClienteTopComponent=TelaCliente Window",
    "HINT_TelaClienteTopComponent=This is a TelaCliente window"
})
public final class TelaClienteTopComponent extends TopComponent {

    Cliente cliente;
    Cliente clienteAnterior;
    SimpleDateFormat sdf;
    DaoCliente daoCliente;

    public TelaClienteTopComponent() {
        cliente = new Cliente();
        clienteAnterior = new Cliente();
        daoCliente = new DaoCliente();
        sdf = new SimpleDateFormat("dd/MM/yyyy");
        initComponents();
        setName(Bundle.CTL_TelaClienteTopComponent());
        setToolTipText(Bundle.HINT_TelaClienteTopComponent());

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfCpf = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfRg = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfEndereco = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfFone = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfDtNasc = new javax.swing.JTextField();
        btNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        btEditar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btPesquisar = new javax.swing.JButton();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.jLabel1.text")); // NOI18N

        tfNome.setText(org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.tfNome.text")); // NOI18N
        tfNome.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.jLabel2.text")); // NOI18N

        tfCpf.setText(org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.tfCpf.text")); // NOI18N
        tfCpf.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.jLabel3.text")); // NOI18N

        tfRg.setText(org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.tfRg.text")); // NOI18N
        tfRg.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.jLabel4.text")); // NOI18N

        tfEndereco.setText(org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.tfEndereco.text")); // NOI18N
        tfEndereco.setEnabled(false);
        tfEndereco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfEnderecoActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.jLabel5.text")); // NOI18N

        tfFone.setText(org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.tfFone.text")); // NOI18N
        tfFone.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.jLabel6.text")); // NOI18N

        tfDtNasc.setText(org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.tfDtNasc.text")); // NOI18N
        tfDtNasc.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(btNovo, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.btNovo.text")); // NOI18N
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btSalvar, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.btSalvar.text")); // NOI18N
        btSalvar.setEnabled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btExcluir, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.btExcluir.text")); // NOI18N
        btExcluir.setEnabled(false);
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btEditar, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.btEditar.text")); // NOI18N
        btEditar.setEnabled(false);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btCancelar, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.btCancelar.text")); // NOI18N
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btPesquisar, org.openide.util.NbBundle.getMessage(TelaClienteTopComponent.class, "TelaClienteTopComponent.btPesquisar.text")); // NOI18N
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(53, 53, 53)
                        .addComponent(tfNome))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(74, 74, 74)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfRg, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(67, 67, 67)
                        .addComponent(tfCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tfDtNasc))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addGap(58, 58, 58)
                            .addComponent(tfFone, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btNovo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btExcluir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btPesquisar)))
                .addGap(0, 10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tfFone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(tfDtNasc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNovo)
                    .addComponent(btSalvar)
                    .addComponent(btExcluir)
                    .addComponent(btEditar)
                    .addComponent(btCancelar)
                    .addComponent(btPesquisar))
                .addGap(0, 11, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tfEnderecoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfEnderecoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfEnderecoActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        // TODO add your handling code here:
        boolean erro = false;
        if (cliente != null) {
            if (tfNome.getText().equals("")) {
                erro = true;
                JOptionPane.showMessageDialog(null, "O nome do cliente é obrigatório.");
            } else {
                try {
                    if (!tfDtNasc.getText().equals("")) {
                        sdf.parse(tfDtNasc.getText());
                    }
                } catch (ParseException ex) {
                    JOptionPane.showMessageDialog(null, "Erro na data, verifique");
                    erro = true;
                    //Exceptions.printStackTrace(ex);
                }
            }
            if (!erro) {
                cliente.setNome(tfNome.getText());
                cliente.setCpf(tfCpf.getText());
                cliente.setEndereco(tfEndereco.getText());
                cliente.setFone(tfFone.getText());
                cliente.setRg(tfRg.getText());
                if (!tfDtNasc.getText().equals("")) {
                    try {
                        cliente.setDataNascimento(sdf.parse(tfDtNasc.getText()));
                    } catch (ParseException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                } else {
                    cliente.setDataNascimento(null);
                }
                cliente = daoCliente.salvar(cliente);

                btNovo.setEnabled(true);
                btSalvar.setEnabled(false);
                btExcluir.setEnabled(true);
                btEditar.setEnabled(true);
                btCancelar.setEnabled(false);
                btPesquisar.setEnabled(true);

                travaCampos();
            }
        }
    }//GEN-LAST:event_btSalvarActionPerformed
    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        // TODO add your handling code here:
        clienteAnterior = cliente;
        cliente = new Cliente();
        atualizaCampos();

        btNovo.setEnabled(false);
        btSalvar.setEnabled(true);
        btExcluir.setEnabled(false);
        btEditar.setEnabled(false);
        btCancelar.setEnabled(true);
        btPesquisar.setEnabled(false);

        destravaCampos();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        // TODO add your handling code here:
        if (cliente != null) {
            try {
                daoCliente.excluir(cliente.getIdCliente());
                cliente = new Cliente();
                atualizaCampos();
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }

            btNovo.setEnabled(true);
            btSalvar.setEnabled(false);
            btExcluir.setEnabled(false);
            btEditar.setEnabled(false);
            btCancelar.setEnabled(false);
            btPesquisar.setEnabled(true);

        }
    }//GEN-LAST:event_btExcluirActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        // TODO add your handling code here:
        clienteAnterior = cliente;

        btNovo.setEnabled(false);
        btSalvar.setEnabled(true);
        btExcluir.setEnabled(false);
        btEditar.setEnabled(false);
        btCancelar.setEnabled(true);
        btPesquisar.setEnabled(false);

        destravaCampos();
    }//GEN-LAST:event_btEditarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        // TODO add your handling code here:
        cliente = clienteAnterior;
        atualizaCampos();

        btNovo.setEnabled(true);
        btSalvar.setEnabled(false);
        if (cliente.getIdCliente() == null) {
            btExcluir.setEnabled(false);
            btEditar.setEnabled(false);
        } else {
            btExcluir.setEnabled(true);
            btEditar.setEnabled(true);
        }
        btCancelar.setEnabled(false);
        btPesquisar.setEnabled(true);

        travaCampos();

    }//GEN-LAST:event_btCancelarActionPerformed

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        // TODO add your handling code here:
        Cliente c = new TelaClientePequisa().getClienteSelecionado();
        if (c != null) {
            cliente = c;
            btEditar.setEnabled(true);
            btExcluir.setEnabled(true);
        }
        atualizaCampos();
    }//GEN-LAST:event_btPesquisarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tfCpf;
    private javax.swing.JTextField tfDtNasc;
    private javax.swing.JTextField tfEndereco;
    private javax.swing.JTextField tfFone;
    private javax.swing.JTextField tfNome;
    private javax.swing.JTextField tfRg;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    public void atualizaCampos() {
        if (cliente != null) {
            tfNome.setText(cliente.getNome());
            tfEndereco.setText(cliente.getEndereco());
            tfFone.setText(cliente.getFone());
            tfRg.setText(cliente.getRg());
            tfCpf.setText(cliente.getCpf());
            if (cliente.getDataNascimento() != null) {
                tfDtNasc.setText(sdf.format(cliente.getDataNascimento()));
            } else {
                tfDtNasc.setText("");
            }
        }
    }

    public void travaCampos() {
        tfNome.setEnabled(false);
        tfEndereco.setEnabled(false);
        tfFone.setEnabled(false);
        tfRg.setEnabled(false);
        tfCpf.setEnabled(false);
        tfDtNasc.setEnabled(false);
    }

    public void destravaCampos() {
        tfNome.setEnabled(true);
        tfEndereco.setEnabled(true);
        tfFone.setEnabled(true);
        tfRg.setEnabled(true);
        tfCpf.setEnabled(true);
        tfDtNasc.setEnabled(true);
    }
}
